home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MovieToolBox / Dialogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-09  |  11.9 KB  |  445 lines  |  [TEXT/KAHL]

  1. /*****************************************************************************************
  2.     File: Dialogs.c
  3.     
  4.     History...
  5.     xxx        RMF    Created from DTS source files, 
  6.                 Created InitSetCompression() routine to eliminate globals
  7.     94/2/9    RMF    Fix up preview window so it did not move or close and reopen, 
  8.                 Add option for next preview image
  9.  
  10. */
  11. #include "gGlobals.h"
  12.  
  13. #include    <pascal.h>
  14. #include    <Scrap.h>
  15. #include    <Desk.h>
  16. #include    <string.h>
  17. #include    <stdio.h>
  18. #include    <Fonts.h>
  19. #include    <Math.h>
  20.  
  21. /*********************************************
  22.  
  23.     Allow the user to set the compression parameters with the standard compression dialog.
  24.     
  25. *********************************************/
  26.  
  27. void InitSetCompression()
  28. {
  29.     gStandardP.flags = scShowMotionSettings;
  30.     gStandardP.theCodecType = kCodecType;
  31.     gStandardP.theCodec = kCodecID;
  32.     gStandardP.spatialQuality = kCodecQuality;
  33.     gStandardP.temporalQuality = 0;
  34.     gStandardP.depth = kCodecDepth;
  35.     gStandardP.frameRate = kFrameRate<<16;
  36.     gStandardP.keyFrameRate = kFrameRate;
  37. }    /* End of () */
  38.  
  39. /*********************************************
  40.  
  41.     Allow the user to set the compression parameters with the standard compression dialog.
  42.     
  43. *********************************************/
  44.  
  45. SetCompression()
  46. {    ComponentInstance ci;
  47.     Point where;
  48.     
  49.     where.h = where.v = -2;
  50.     ci = OpenDefaultComponent(StandardCompressionType,0);
  51.     if ( ci == nil )  {
  52.         Error("Couldn't open StdCompression",0);    return;
  53.     }
  54.  
  55.     SCGetCompression(ci,&gStandardP,where);
  56.     
  57. }    /* End of () */
  58.  
  59. /***********************************************************************************
  60.  
  61.     Allow the user to set the number of stages she wants the processing to take.
  62. */
  63. SetStages()
  64. {
  65. #define    STAGE_DLOG    128
  66. #define    IT_STAGES    3
  67. #define    IT_LINEAR    4
  68. #define    IT_OK    1
  69. #define    IT_CANCEL    2
  70.  
  71.     CGrafPtr     savePort;
  72.     DialogPtr    theDialog;
  73.     short        itemType;
  74.     Handle        itemHdl;
  75.     Rect        itemRect;
  76.     short        itemHit,lastItem = -1;
  77.     Boolean        done = false;
  78.     Point         tp;
  79.     GDHandle    saveGD;
  80.     Str255        text;
  81.     long        num;
  82.     long        stageCount = gNumberSteps;
  83.     Boolean        linear = true;
  84.     
  85.     if  ((theDialog = GetNewDialog(STAGE_DLOG, nil, (WindowPtr) -1)) == nil )
  86.         return;
  87.  
  88.     GetMouse(&tp);
  89.     GetGWorld(&savePort,&saveGD);
  90.     SetPort(theDialog);
  91.     GetDItem(theDialog,IT_OK,&itemType,&itemHdl,&itemRect);
  92.     PenSize(3,3);
  93.     InsetRect(&itemRect,-4,-4);
  94.     FrameRoundRect(&itemRect,16,16);
  95.  
  96.     
  97.     GetDItem(theDialog,IT_STAGES,&itemType,&itemHdl,&itemRect);
  98.     NumToString(stageCount, text);
  99.     SetIText(itemHdl,text);
  100.  
  101.     SelIText(theDialog,IT_STAGES,0,32767);
  102.  
  103.     GetDItem(theDialog,IT_LINEAR,&itemType,&itemHdl,&itemRect);
  104.     SetCtlValue((ControlHandle)itemHdl,linear);
  105.     HiliteControl((ControlHandle)itemHdl,255);
  106.     
  107.     while ( !done ) {
  108.         ModalDialog(nil, &itemHit);
  109.         if ( itemHit != lastItem ) {
  110.             switch( lastItem ) { 
  111.             case IT_STAGES :
  112.                 GetDItem(theDialog,IT_STAGES,&itemType,&itemHdl,&itemRect);
  113.                 GetIText(itemHdl,text);
  114.                 StringToNum(text, &num);
  115.                 if ( num > 0  ) stageCount = num;
  116.                 else {    SysBeep(1);
  117.                     NumToString(stageCount, text);
  118.                     SetIText(itemHdl,text);
  119.                     if (itemHit == IT_OK) itemHit = -1;
  120.                     }
  121.                 break;
  122.             }
  123.             lastItem = itemHit;
  124.         }
  125.         
  126.         switch  ( itemHit  ) {
  127.         case IT_OK :
  128.             gNumberSteps = stageCount;
  129.             done = true;
  130.             break;
  131.         case IT_CANCEL :    done = true;    break;
  132.         case IT_LINEAR:
  133.             linear = !linear;
  134.             GetDItem(theDialog,IT_LINEAR,&itemType,&itemHdl,&itemRect);
  135.             SetCtlValue((ControlHandle)itemHdl,linear);
  136.             break;
  137.         }
  138.     }
  139.     CloseDialog(theDialog);
  140.     SetGWorld(savePort,saveGD);
  141. }    /* End of () */
  142.  
  143. /********************************************
  144.  
  145.     Allocate the image buffers for the processing,
  146.     when the 1st Picture window is opened!
  147.     
  148.     Creates the following GWorld offscreen buffers:
  149.         gDstWorld, gBufferWorld, gWorld, gAltWorld
  150.         
  151.     Note: Try to allocated from program memory, 
  152.           if none try Finder's temp memory!
  153. ********************************************/
  154.  
  155. OSErr AllocateBuffers()
  156. {    Rect    rect = gOriginalPicFrame;
  157.     OSErr result = 0;
  158.     short depth = gStandardP.depth  > 32 ? (gStandardP.depth-32) : gStandardP.depth;
  159.     CTabHandle    clut = nil;
  160.     
  161.     OffsetRect(&rect,-rect.left,-rect.top);
  162.     
  163.     if ( gStandardP.depth > 32 ) 
  164.         clut = GetCTable(gStandardP.depth);
  165.         
  166.     if ( gDstWorld ) {                    /* Area used to create the image to be added to movie */
  167.         DisposeGWorld(gDstWorld);        gDstWorld = nil;
  168.         }
  169.         
  170.     if ( gWorld ) {                        /* Area used to hold the source image */
  171.         DisposeGWorld(gWorld);            gWorld = nil;
  172.         }
  173.         
  174.     if ( gAltWorld ) {                    /* Area used to hole the 2nd image of crossfades */
  175.         DisposeGWorld(gAltWorld);        gAltWorld = nil;
  176.         }
  177.         
  178.     if ( gBufferWorld ) {                /* used to preform oversampling */
  179.         DisposeGWorld(gBufferWorld);    gBufferWorld = nil;
  180.         }
  181.         
  182.     if ( (result=NewGWorld(&gWorld,depth,&rect,clut,nil,0)) != 0 ) {
  183.         if ( (result=NewGWorld(&gWorld,depth,&rect,clut,nil,useTempMem)) != 0 ) {
  184.             Error("NewGWorld Failed",result);    goto done;
  185.             }
  186.     }
  187.     if ( (result=NewGWorld(&gAltWorld,depth,&rect,clut,nil,0)) != 0 ) {
  188.         if ( (result=NewGWorld(&gAltWorld,depth,&rect,clut,nil,useTempMem)) != 0 ) {
  189.             Error("NewGWorld Failed",result);    goto done;
  190.             }
  191.     }
  192.     if ( (result=NewGWorld(&gDstWorld,depth,&rect,clut,nil,0)) != 0 ) {
  193.         if ( (result=NewGWorld(&gDstWorld,depth,&rect,clut,nil,useTempMem)) != 0 ) {
  194.             Error("NewGWorld Failed",result);    goto done;
  195.             }
  196.     }
  197.  
  198.     if ( (result=NewGWorld(&gBufferWorld,depth,&rect,clut,nil,0)) != 0 ) {
  199.         if ( (result=NewGWorld(&gBufferWorld,depth,&rect,clut,nil,useTempMem)) != 0 ) {
  200.             Error("NewGWorld Failed",result);    goto done;
  201.             }
  202.     }
  203.     
  204. done:
  205.     if ( result ) {            /* Errors free buffers */
  206.         if ( gWorld )        DisposeGWorld(gWorld);
  207.         if ( gAltWorld )    DisposeGWorld(gAltWorld);
  208.         if ( gDstWorld )    DisposeGWorld(gDstWorld);
  209.         if ( gBufferWorld )    DisposeGWorld(gBufferWorld);
  210.         gWorld= nil;
  211.         gAltWorld= nil;
  212.         gDstWorld= nil;
  213.         gBufferWorld= nil;
  214.         }
  215.     if ( clut )                DisposeCTable(clut);
  216.     return(result);        
  217. }    /* End of () */
  218.  
  219.  
  220. /*********************************************
  221.  
  222.     Show a preview of some stage in the process.
  223.     
  224. ********************************************/
  225. void DoPreview(Boolean dialog)
  226. {
  227. #define    PV_DLOG    129
  228. #define    IT_STAGE    4
  229. #define    IT_OK    1
  230. #define    IT_CANCEL    2
  231.  
  232.     CGrafPtr     savePort;
  233.     DialogPtr    theDialog;
  234.     short        itemType;
  235.     Handle        itemHdl;
  236.     Rect        itemRect;
  237.     short        itemHit,lastItem = -1;
  238.     Boolean        done = false;
  239.     Point         tp;
  240.     GDHandle    saveGD;
  241.     long        num;
  242.     Str31        numStr;
  243.     
  244. static long    whichStage = 15;
  245.     
  246.     if (!gDstWorld || !gWorld || !gAltWorld) { SysBeep(0); return; }
  247.     
  248. if (dialog) {
  249.     
  250.     if  ((theDialog = GetNewDialog(PV_DLOG, nil, (WindowPtr) -1)) == nil )
  251.         return;
  252.  
  253.     GetMouse(&tp);
  254.     GetGWorld(&savePort,&saveGD);
  255.     SetPort(theDialog);
  256.     GetDItem(theDialog,IT_OK,&itemType,&itemHdl,&itemRect);
  257.     PenSize(3,3);    InsetRect(&itemRect,-4,-4);    FrameRoundRect(&itemRect,16,16);
  258.  
  259.     GetDItem(theDialog,IT_STAGE,&itemType,&itemHdl,&itemRect);
  260.     NumToString(whichStage, numStr);    SetIText(itemHdl,numStr);
  261.  
  262.     SelIText(theDialog,IT_STAGE,0,32767);
  263.  
  264.     while ( !done ) {
  265.         ModalDialog(nil, &itemHit);
  266.         
  267.         if ( itemHit != lastItem ) {        /* Check number, if user goes to another control */
  268.             switch( lastItem ) { 
  269.             case IT_STAGE :
  270.                 GetDItem(theDialog,IT_STAGE,&itemType,&itemHdl,&itemRect);
  271.                 GetIText(itemHdl,numStr);
  272.                 StringToNum(numStr, &num);
  273.                 if ( num > 0  && num <= gNumberSteps ) 
  274.                     whichStage = num - 1;
  275.                 else    {
  276.                     SysBeep(1);
  277.                     NumToString(whichStage,numStr);
  278.                     SetIText(itemHdl,numStr);
  279.                     if (itemHit == IT_OK) itemHit = -1;
  280.                     }
  281.                 break;
  282.             }
  283.             lastItem = itemHit;
  284.         }
  285.         
  286.         switch  ( itemHit  ) {
  287.         case IT_OK :    done = true;
  288.             break;
  289.         case IT_CANCEL :    whichStage = 0;
  290.             done = true;
  291.             break;
  292.         }
  293.     }
  294.     CloseDialog(theDialog);
  295. } else {    /* End if Dialog */
  296.         whichStage++; 
  297.         if (whichStage >= gNumberSteps) whichStage = 0;
  298.         }
  299.                 
  300.     itemRect = gSrcWindow->portRect;
  301.     SetGWorld(gSrcWindow,nil);
  302.     LocalToGlobal((Point *)&itemRect.top);    LocalToGlobal((Point *)&itemRect.bottom);
  303.     OffsetRect(&itemRect,20,20);
  304.     if ( gOversample ) {
  305.         itemRect.right = itemRect.left + (itemRect.right-itemRect.left)/2;
  306.         itemRect.bottom = itemRect.top + (itemRect.bottom-itemRect.top)/2;
  307.         }
  308.         
  309.     SetGWorld(savePort,saveGD);
  310.     if ( gDstWindow ) {                    /* Use open Dest. Window,  if same size */
  311.         Boolean same;
  312.         
  313.         OffsetRect(&itemRect,-itemRect.left,-itemRect.top);
  314.         same = EqualRect(&itemRect, &gDstWindow->portRect);
  315.         LocalToGlobal((Point *)&itemRect.top);    LocalToGlobal((Point *)&itemRect.bottom);
  316.         if (!same) {
  317.             CloseWindow((WindowPtr)gDstWindow);    gDstWindow = nil;
  318.              }
  319.         } 
  320.         
  321.     if (!gDstWindow )
  322.         if ( (gDstWindow = (CWindowPtr)NewCWindow(nil,&itemRect,"\pPreview",true,
  323.                                                     zoomDocProc,(WindowPtr)-1,true,0)) == nil ) {
  324.             Error("NewCWindow Failed",0);    goto done;
  325.             }
  326.         
  327.     BringToFront((WindowPtr)gDstWindow);
  328.     HiliteWindow((WindowPtr)gDstWindow,true);
  329.     SetGWorld((CGrafPtr)gDstWorld,nil);    
  330.     PaintRect(&gDstWorld->portRect);
  331.  
  332.     // Assume: if ( gDstWorld && gWorld && gAltWorld ) 
  333.     DoStage(whichStage,gNumberSteps,gReverse);
  334.     
  335.     if (OptionDown())    gFrameNumber = whichStage;        /* Add Frame Number to Preview */
  336.     SetGWorld((CGrafPtr)gDstWindow,nil);
  337.     InvalRect(&gDstWindow->portRect);
  338.     DoUpdate(gDstWindow);
  339.     gFrameNumber = -1;
  340.     
  341. done:
  342.     SetGWorld(savePort,saveGD);
  343. }    /* End of () */
  344.  
  345. /********************************************
  346.  
  347.     Make a source window 
  348.     
  349. ********************************************/
  350.  
  351. CWindowPtr    MakeSWindow(Rect *frame,StringPtr name,GWorldPtr buffer)
  352. {    CWindowPtr    wind;
  353.  
  354.     if ( (wind = (CWindowPtr)NewCWindow(nil,frame,name,false, zoomDocProc,(WindowPtr)-1,true,0)) == nil ) {
  355.         Error("NewCWindow Failed",0);    return(nil);
  356.         }
  357.     ShowWindow((WindowPtr)wind);
  358.     BringToFront((WindowPtr)wind);    HiliteWindow((WindowPtr)wind,true);
  359.     SetWRefCon((WindowPtr)wind,(long)buffer);
  360.     return(wind);
  361. }    /* End of () */
  362.  
  363. /********************************************
  364.  
  365.     Open a file.
  366.     
  367. ********************************************/
  368. OSErr    DoOpen(FSSpec *fsp)
  369. {    long        result = noErr;
  370.     Rect         zpFrame;
  371.     GWorldPtr    saveWorld;
  372.     GDHandle    saveGD;
  373.     short        oFile = -1;
  374.     StandardFileReply    theSFR;    
  375.     Boolean HasCompression = true;
  376.     long resp;
  377.     
  378.     if ( Gestalt(gestaltCompressionMgr, &resp) != noErr || resp < 15 )    HasCompression = false;
  379.  
  380.     GetGWorld(&saveWorld,&saveGD);
  381.     if ( fsp == nil ) {
  382.         SFTypeList     types = { 'PICT','JFIF',0 };
  383.         if ( gHasNewStdFile ) {
  384.             if (!HasCompression)    StandardGetFile(nil,2,types,&theSFR);
  385.             else                    StandardGetFilePreview(nil,2,types,&theSFR);
  386.         } else {
  387.             SFReply        osfr;
  388.             Point    pt = {100,100};
  389.             
  390.             SFGetFile(pt,(ConstStr255Param)"",nil,1,types,nil,&osfr);
  391.             theSFR.sfGood = osfr.good;
  392.             theSFR.sfReplacing = osfr.copy;
  393.             theSFR.sfType = osfr.fType;
  394.             if ( osfr.good )    FSMakeFSSpec(osfr.vRefNum,0L,osfr.fName,&theSFR.sfFile);
  395.             }
  396.         if ( !theSFR.sfGood  )    return(1);
  397.     } else {
  398.         theSFR.sfFile = *fsp;
  399.         }
  400.         
  401.     if ( FSpOpenDF(&theSFR.sfFile,fsRdPerm,&oFile) ) {
  402.         result = -1;    goto done;
  403.         }
  404.  
  405.     
  406.     /************************************************
  407.      *    Get the picture frame, to see how big of a window to make.
  408.      ************************************************/
  409.  
  410.     if ( GetPictureFileHeader(oFile,&gOriginalPicFrame,nil ) ) {
  411.         result = -1;    goto done;
  412.         }
  413.  
  414.     if ( gSrcWindow == nil ) {                                /* Open Image 1's window */
  415.         OffsetRect(&gOriginalPicFrame,-gOriginalPicFrame.left,-gOriginalPicFrame.top);
  416.         if ( gOversample ) {
  417.             gOriginalPicFrame.right *= 2;
  418.             gOriginalPicFrame.bottom *= 2;
  419.             }
  420.         zpFrame = gOriginalPicFrame;
  421.     
  422.         if ( (result=AllocateBuffers()) != 0 )    goto done;    /* Make GWorld buffers,   */
  423.                                                             /* if no windows open yet */
  424.         
  425.         OffsetRect(&zpFrame,50,50);
  426.         gSrcWindow = MakeSWindow(&zpFrame,theSFR.sfFile.name,gWorld);
  427.         SetGWorld((CGrafPtr)gWorld,nil);
  428.         if ( (result=DrawPictureFile(oFile,&gWorld->portRect,nil)) != 0 ) {
  429.             Error("DrawPictureFile Failed",result);    goto done;
  430.             }
  431.             
  432.     } else {                                                /* Open Image 2's window */
  433.         zpFrame = gOriginalPicFrame;
  434.         OffsetRect(&zpFrame,70,70);
  435.         gAltWindow = MakeSWindow(&zpFrame,theSFR.sfFile.name,gAltWorld);
  436.         SetGWorld((CGrafPtr)gAltWorld,nil);
  437.         if ( (result=DrawPictureFile(oFile,&gAltWorld->portRect,nil)) != 0 ) {
  438.             Error("DrawPictureFile Failed",result);    goto done;
  439.             }
  440.     }
  441. done:
  442.     if ( oFile != -1 )    FSClose(oFile);
  443.     SetGWorld(saveWorld,saveGD);
  444.     return(result);
  445. }    /* End of () */